package com.dragosh.seeker;

import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class DroidTooth extends Activity {
	private static final int REQUEST_ENABLE_BT = 0;
	private ArrayAdapter<String> mArrayAdapter;
	private final String Logcat = "Deb";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		TextView tv = new TextView(this);
		tv.setText("Hello, Android");
		setContentView(tv);

		Log.v(Logcat, "1gat");

		//Local Bluetooth Adaptor
		final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
				.getDefaultAdapter();
		if (mBluetoothAdapter == null) {
			// Device does not support Bluetooth
			tv.setText("Bluetooth not available");
			setContentView(tv);
		} else {
			//Adaptor instantiated
			tv.setText("Nexus One is equipped with Bluetooth technology");
			setContentView(tv);

		}
		// Enable Bluetooth
		if (!mBluetoothAdapter.isEnabled()) {
			Intent enableBtIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
		}

		/*
		 * Enable device discoverability
		 * 
		 * Intent discoverableIntent = new
		 * Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
		 * discoverableIntent
		 * .putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
		 * startActivity(discoverableIntent);
		 */

		/* Production level code */

		//Get a set of BluetoothDevice objects representing all paired devices
		Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
				.getBondedDevices();

		// Log.v(Logcat,"1gat");

		// If there are paired devices
		if (pairedDevices.size() > 0) {
			// Loop through paired devices
			for (BluetoothDevice device : pairedDevices) {
				// Add the name and address to an array adapter to show in a
				// ListView
				mArrayAdapter
						.add(device.getName() + "\n" + device.getAddress());

			}
		}

		String dStarted = BluetoothAdapter.ACTION_DISCOVERY_STARTED;
		String dFinished = BluetoothAdapter.ACTION_DISCOVERY_FINISHED;
		//Object that will receive intents sent by sendBroadcast()
		BroadcastReceiver discoveryMonitor = new BroadcastReceiver() {
			String dStarted = BluetoothAdapter.ACTION_DISCOVERY_STARTED;
			String dFinished = BluetoothAdapter.ACTION_DISCOVERY_FINISHED;

			@Override
			public void onReceive(Context context, Intent intent) {
				if (dStarted.equals(intent.getAction())) {
					//If receiver gets "Discovery Started" action from the Intent we display a message 	
					Toast.makeText(getApplicationContext(),
							"Discovery	Started	. . .	", Toast.LENGTH_SHORT)
							.show();
				} else if (dFinished.equals(intent.getAction())) {
					// If receiver gets "Discovery Finished" action from the Intent we display a message 	
				}
			}
		};
		// Discovery has completed.
		Toast.makeText(getApplicationContext(), "Discovery	Completed	. . .	",
				Toast.LENGTH_SHORT).show();
		//Dynamic registration of the receiver
		registerReceiver(discoveryMonitor, new IntentFilter(dStarted));
		registerReceiver(discoveryMonitor, new IntentFilter(dFinished));

		/*
		 * tv.setText("It worked"); setContentView(tv);
		 */
		//New receiver for Bluetooth discovery results
		BroadcastReceiver discoveryResult = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {

				//Trying to obtain device name from Intent object
				String remoteDeviceName = intent
						.getStringExtra(BluetoothDevice.EXTRA_NAME);
				//Trying to obtain BluetoothDevice object from Intent object
				BluetoothDevice remoteDevice;
				remoteDevice = intent
						.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				//Display results
				Toast.makeText(getApplicationContext(),
						"Discovered: " + remoteDeviceName, Toast.LENGTH_SHORT)
						.show(); // TODO Do something with the remote Bluetooth
									// Device.

			}
		};

		registerReceiver(discoveryResult, new IntentFilter(
				BluetoothDevice.ACTION_FOUND));
		if (!mBluetoothAdapter.isDiscovering())
			mBluetoothAdapter.startDiscovery();

		/*
		 * To be continued ... connecting phones
		 */

	}
}